package top.ed333.mcplugins.configurableevents;

import net.milkbowl.vault.economy.Economy;
import net.milkbowl.vault.permission.Permission;
import org.bukkit.OfflinePlayer;
import org.bukkit.plugin.RegisteredServiceProvider;
import org.jetbrains.annotations.NotNull;

import static org.bukkit.Bukkit.getServer;

public class VaultHelper {
    private static Economy econ;
    private static Permission perms;

    public static boolean setupVault() {
        boolean result = false;
        if (getServer().getPluginManager().getPlugin("Vault") != null) {
            RegisteredServiceProvider<Economy> economyProvider = getServer().getServicesManager().getRegistration(Economy.class);
            if (economyProvider != null) {
                econ = economyProvider.getProvider();
            }

            RegisteredServiceProvider<Permission> permissionProvider = getServer().getServicesManager().getRegistration(Permission.class);
            if (permissionProvider != null) {
                perms = permissionProvider.getProvider();
            }
            result = economyProvider != null && permissionProvider != null;
        }
        return result;
    }

    public static String getPlayerGroup(@NotNull OfflinePlayer player) {
        return perms.getPrimaryGroup(player.getPlayer());
    }

    public static Economy getEcon() {
        return econ;
    }

    // 获取玩家存款
    public static double getBalance(OfflinePlayer player) {
        return econ.getBalance(player);
    }

    // 存款
    public static void depositPlayer(OfflinePlayer player, double amt) {
        econ.depositPlayer(player, amt);
    }

    // 取款
    public static void withDrawPlayer(OfflinePlayer player, double amt) {
        econ.withdrawPlayer(player, amt);
    }
}
